package com.atguigu.gmall.all.controller;


import com.atguigu.gmall.cart.client.CartInfoFeignClient;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author gsy
 * @date 2021年09月06日21:09
 * Description
 */
@Controller
public class CartInfoController {

    @Autowired
    private CartInfoFeignClient cartInfoFeignClient;

    //加入购物车
    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes){
        //调用购物微服务加入购物车
        cartInfoFeignClient.addCart(skuId,skuNum);
        //重定向到商品添加完成页面
        redirectAttributes.addAttribute("pId",skuId);
        redirectAttributes.addAttribute("rcd",skuNum);
        return "redirect:http://cart.gmall.com/addToCart.html";


    }
    //商品已成功加入购物车
    @GetMapping("/addToCart.html")
    public String toCart(Long pId, Integer rcd, Model model){
        //查询需要回显的商品
        CartInfo cartInfo = cartInfoFeignClient.findCartInfo(pId);
        //修改数量为添加的数量
        cartInfo.setSkuNum(rcd);
        model.addAttribute("cartInfo",cartInfo);
        return "cart/addCart";
    }
    //去购物车页面
    @GetMapping("/cart.html")
    public String cart(){

        return "cart/index";

    }
}
